//
//  HJStatusPictureView.swift
//  Weibo
//
//  Created by 韩佳 on 16/4/22.
//  Copyright © 2016年 ddg. All rights reserved.
//

import UIKit

class HJStatusPictureView: UICollectionView {
    private let reuseID:String = "pictureCell"
    //元组属性
    var dataInfo:(pic_urls:[HJStatusPhontoInfo],size:CGSize)?{
        didSet{
            if dataInfo?.pic_urls.count == 1 {
                let layout = collectionViewLayout as! UICollectionViewFlowLayout
                layout.itemSize = dataInfo?.size ?? CGSizeZero
            }else{
                let layout = collectionViewLayout as! UICollectionViewFlowLayout
                layout.itemSize = CGSizeMake(HJStatusPictureItemWH, HJStatusPictureItemWH)
            }
            self.snp_updateConstraints(closure: { (make) -> Void in
                make.size.equalTo(dataInfo?.size ?? CGSizeZero)
            })
            reloadData()
        }
    }
    override init(frame: CGRect, collectionViewLayout layout: UICollectionViewLayout) {
        super.init(frame: frame, collectionViewLayout: layout)
        setupUI()
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    private func setupUI(){
        backgroundColor = UIColor(white: 242/255.0, alpha: 1)
        //注册cell
        registerClass(HJStatusPictureViewCell.self, forCellWithReuseIdentifier: reuseID)
        //设置item
        let layout = self.collectionViewLayout as! UICollectionViewFlowLayout
        layout.itemSize = CGSize(width: HJStatusPictureItemWH, height: HJStatusPictureItemWH)
        layout.minimumInteritemSpacing = HJStatusPictureItemMargin
        layout.minimumLineSpacing = HJStatusPictureItemMargin
        
        //设置数据源代理 因为数据在自己身上所以可以设置代理为自己
        self.dataSource = self
        addSubview(pictureCountLabel)
        pictureCountLabel.snp_makeConstraints { (make) -> Void in
            make.center.equalTo(self)
        }
    }
    var pic_urls: [HJStatusPhontoInfo]?{
        didSet{
            pictureCountLabel.text = "\(pic_urls?.count ?? 0)"
            reloadData()
        }
    }
    private lazy var pictureCountLabel = UILabel()
}

extension HJStatusPictureView:UICollectionViewDataSource{
    func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return pic_urls?.count ?? 0
    }
    
    func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCellWithReuseIdentifier(reuseID, forIndexPath: indexPath) as! HJStatusPictureViewCell
//        cell.backgroundColor = UIColor.orangeColor()
        cell.photoInfo = pic_urls![indexPath.item]
        return cell
    }
}


class HJStatusPictureViewCell:UICollectionViewCell {
    var photoInfo:HJStatusPhontoInfo?{
        didSet{
            imageView.sd_setImageWithURL(NSURL(string: photoInfo?.thumbnail_pic ?? ""), placeholderImage: UIImage(named: "timeline_image_placeholder"))
            gifImageView.hidden = !(photoInfo?.thumbnail_pic ?? "").hasSuffix(".gif")
        }
    }
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        setupUI()
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    private func setupUI(){
        contentView.addSubview(imageView)
        contentView.addSubview(gifImageView)
        
        imageView.snp_makeConstraints { (make) -> Void in
            make.edges.equalTo(contentView)
        }
        gifImageView.snp_makeConstraints { (make) -> Void in
            make.bottom.trailing.equalTo(contentView)
        }
    }
    
    private lazy var imageView:UIImageView = {
        let imgView = UIImageView(image: UIImage(named: "timeline_image_placeholder"))
        imgView.contentMode = UIViewContentMode.ScaleAspectFill
        imgView.clipsToBounds = true
        
        return imgView
    }()
    
    private lazy var gifImageView:UIImageView = UIImageView(image: UIImage(named: "timeline_image_gif"))
}











